package com.example.cdctest.util;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

public class MailUtil {
    private MailUtil(){}
    private static MailUtil mailUtil = new MailUtil();
    public static MailUtil getMailUtil(){return mailUtil;}
    /**
     * 发送邮件
     * @param subject  邮件主题
     * @param content  邮件内容（支持HTML）
     * @param toEmailAddres  收件人
     * @throws Exception
     * @author Monk
     * @date 2019年5月22日 下午6:27:27
     */
    public void sendMail(String subject, String content, String toEmailAddres) throws Exception {
        String host = "smtp.qq.com";       		 	//邮箱服务器地址
        String port = "587";
        String auth = "true";                     	//是否需要进行身份验证,视调用的邮箱而定，比方说QQ邮箱就需要，否则就会发送失败
        String protocol = "smtp";                  	//传输协议
        String mailFrom = "2867143325@qq.com";   	//发件人邮箱
        String personalName = "Blog官方客服";         //发件人邮箱别名
        String username = "2867143325@qq.com";   	//发件人邮箱用户名
        String password = "rrvhubbipuxddeib";      	//发件人邮箱密码
        String mailDebug = "false";                	//是否开启debug
        String contentType = "text/html;charset=UTF-8";                 	//邮件正文类型

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", auth == null ? "true" : auth);
        props.put("mail.transport.protocol", protocol == null ? "smtp" : protocol);
        props.put("mail.smtp.port", port == null ? "25" : port);
        props.put("mail.debug", mailDebug == null ? "false" : mailDebug);
        Session mailSession = Session.getInstance(props);

        // 设置session,和邮件服务器进行通讯。
        MimeMessage message = new MimeMessage(mailSession);
        // 设置邮件主题
        message.setSubject(subject);
        // 设置邮件正文
        message.setContent(content, contentType == null ? "text/html;charset=UTF-8" : contentType);
        // 设置邮件发送日期
        message.setSentDate(new Date());
        InternetAddress address = new InternetAddress(mailFrom, personalName);
        // 设置邮件发送者的地址
        message.setFrom(address);
        // 设置邮件接收方的地址
        message.setRecipients(Message.RecipientType.TO, toEmailAddres);
        Transport transport = null;
        transport = mailSession.getTransport();
        message.saveChanges();

        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    //返回邮箱内容
    public String getMailContent(String vercode) {
        StringBuilder sb=new StringBuilder();
        sb.append("<html>");
        sb.append("<h4>尊敬的用户您好：</h4>");
        sb.append("<br/><span>您邮箱验证的验证码为：<i style='color:#00F'>");
        sb.append(vercode);
        sb.append("</i>，请在页面中完成输入</span>");
        sb.append("</html>");
        return sb.toString();
    }
    public String createFoursRandom() {
        String vercode="";
        while(vercode.length()<4) {
            Random r=new Random();
            int int_code=r.nextInt(123);
            if((int_code>=65&&int_code<=90)||(int_code>=97&&int_code<=122)||(int_code>=48&&int_code<=57)) {
                vercode+=(char)int_code;
            }
        }
        return vercode;
    }
}
